<template>
  <div>
    <h1>品牌列表</h1>
  </div>
  <el-form :model="searchForm" :inline="true">
    <el-form-item label="请输入品牌名称">
      <el-input v-model="searchForm.keyWord" clearable></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="loadList">搜索</el-button>
    </el-form-item>
  </el-form>
  <el-form-item>
    <el-button type="primary" @click="toAdd()">添加品牌</el-button>
  </el-form-item>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="brandId" label="品牌id" width="180" />
      <el-table-column prop="brandName" label="品牌名称" width="180" />

      <el-table-column prop="brandUse" label="使用数量" />
      <el-table-column prop="brandTop" label="排序" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="brandStatus" label="品牌状态 ">
        <template #default="scope">
          <el-tooltip
            :content="
              '状态: ' + (scope.row.brandStatus === '0' ? '显示' : '隐藏')
            "
            placement="top"
          >
            <el-switch
              v-model="scope.row.brandStatus"
              style="
                --el-switch-on-color: #13ce66;
                --el-switch-off-color: #ff4949;
              "
              :active-value="0"
              :inactive-value="1"
              active-text="显示"
              inactive-text="隐藏"
              @click="updateStatus1(scope.row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人 " />
      <el-table-column prop="createTime" label="创建时间 " />

      <el-table-column label="操作 ">
        <el-button type="success">编辑</el-button>
      </el-table-column>
    </el-table>
  </div>

  <div class="demo-pagination-block">
    <el-pagination
      v-model:current-page="searchForm.pageNum"
      v-model:page-size="searchForm.pageSize"
      :page-sizes="[1, 2, 3, 4]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

  <el-dialog v-model="dialogFormVisible" title="添加品牌" width="500">
    <el-form :model="saveForm" :rules="rules" ref="ruleFormRef">
      <el-form-item
        label="品牌名称"
        :label-width="formLabelWidth"
        prop="brandName"
      >
        <el-input v-model="saveForm.brandName" autocomplete="off" />
      </el-form-item>
      <el-form-item
        label="品牌使用"
        :label-width="formLabelWidth"
        prop="brandUse"
      >
        <el-input v-model="saveForm.brandUse" autocomplete="off" />
      </el-form-item>
      <el-form-item label="排序" :label-width="formLabelWidth" prop="brandTop">
        <el-input v-model="saveForm.brandTop" autocomplete="off" />
      </el-form-item>
      <el-form-item
        label="品牌状态"
        :label-width="formLabelWidth"
        prop="brandStatus"
      >
        <el-radio-group v-model="saveForm.brandStatus">
          <el-radio :value="1">显示</el-radio>
          <el-radio :value="0">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="SaveBrand(ruleFormRef)">
          确认添加
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import type { ComponentSize } from "element-plus";
const size = ref<ComponentSize>("default");
const background = ref(false);
const disabled = ref(false);
import { onMounted, ref } from "vue";
const tableData = ref([]);
import { brandList, saveBrand, updateStatus } from "@/api/api";
import { ElMessage } from "element-plus";
import { ta } from "element-plus/es/locales.mjs";
const total = ref(0);
const searchForm = ref({
  pageSize: 3,
  pageNum: 1,
  keyWord: "",
});
const loadList = () => {
  brandList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("展示成功");
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};
onMounted(() => {
  loadList();
});

const dialogTableVisible = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const saveForm = ref({
  brandName: "",
  brandUse: null,
  brandTop: null,
  brandStatus: null,
  createBy: "",
  createTime: "",
  updateBy: "",
  updateTime: "",
});
const toAdd = () => {
  saveForm.value = {
    brandName: "",
    brandUse: null,
    brandTop: null,
    brandStatus: null,
    createBy: "",
    createTime: "",
    updateBy: "",
    updateTime: "",
  };
  dialogFormVisible.value = true;
};

const SaveBrand = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      saveBrand(saveForm.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const updateStatus1 = (row: any) => {
  updateStatus(row.brandId).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
import { type FormInstance, type FormRules } from "element-plus";
const ruleFormRef = ref<FormInstance>();

const rules = ref<FormRules>({
  brandName: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
  brandUse: [{ required: true, message: "请输入使用量", trigger: "blur" }],
  brandTop: [{ required: true, message: "请输入排名", trigger: "blur" }],
  brandStatus: [{ required: true, message: "请选择状态", trigger: "blur" }],
});
</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
